import pandas as pd
from pandas import DataFrame

class TrainAction (): #Train_Action 行为数据集
    
    def __init__(self,mode) -> None:
        self.Data = None #数据
        self.actions = [] #行为列表
        self.solutions = [] #解决方式列表
        self.activatings = [] #诱因列表
        self.solution_start = 0 #数据集中解决方式开始列索引
        self.col = 0 #数据集的列数
        self.datasize = 0 #数据集的行数

        path = '../database/' + mode + '_TrainData_Action.csv'
        self.Data = pd.read_csv(path)

        self.actions = ['话变多','话变少','面带微笑','来回走动','突然决定出去玩','面无表情','食欲增加',\
        '食欲减少','说话声音变响','说话声音变轻','浑身颤抖']

        self.solutions = ['让您的另一半休息一会儿','陪您的另一半一起锻炼','听您的另一半倾诉烦恼',\
        '陪您的另一半通过写作、绘画等方式宣泄情绪','好好安慰您的另一半','陪您的另一半一起听音乐',\
        '陪您的另一半出门散心','陪您的另一半一起工作','让您的另一半自己冷静一会儿',\
        '陪您的另一半一起做一些兴趣爱好']

        self.activatings = ['您的另一半很努力，但却得不到身边人的认可','您的另一半一连遇到了好多好多倒霉的事情',\
        '您的另一半生活和工作困难重重，压力很大','您的另一半看见通过不当手段牟取利益的人位高权重',\
        '您的另一半看到了一些糟糕的事情发生','您的另一半缺乏归属感和被爱的感觉','您的另一半感觉自己没有被别人尊重',\
        '您的另一半感觉自己受到了威胁','您的另一半对自己期待之事的结果不确定','无原因']

        self.solution_start = 16

        self.col = self.Data.shape[1]
        self.datasize = self.Data.shape[0]


    def Update (self,actions,solutions,activatings): #添加行为、解决方式、诱因

        for i in range (0,len(actions)):
            self.actions.append(actions[i])
        
        for i in range (0,len(solutions)):
            self.solutions.append(solutions[i])

        for i in range (0,len(activatings)):
            self.activatings.append(activatings[i])

        self.solution_start += len(actions)

        self.col = self.col + len(actions) + len(solutions)


    def AddAction (self,actions): #添加行为
        former = self.Data.iloc[:,0:self.solution_start]
        latter = self.Data.iloc[:,self.solution_start:self.col]
        Nactions = []

        for i in range (0,len(actions)):
            count = 0
            for j in range (0,self.col):
                if actions[i] == self.Data.iloc[:,j].name:
                    break
                count += 1
            if count < self.col: #有重复
                continue
            Nactions.append(actions[i])

        Nactions = list (set(Nactions)) #去除重复

        for i in range (0,len(Nactions)):
            former[Nactions[i]] = 0
        
        self.Data = pd.concat([former,latter],axis=1)

        self.Update(Nactions,[],[])


    def AddSolution (self,solutions): #添加解决方法
        Nsolutions = []

        for i in range (0,len(solutions)):
            count = 0
            for j in range (0,self.col):
                if solutions[i] == self.Data.iloc[:,j].name:
                    break
                count += 1
            if count < self.col: #有重复
                continue
            Nsolutions.append(solutions[i])

        Nsolutions = list (set(Nsolutions)) #去除重复

        for i in range (0,len(Nsolutions)):
            self.Data[Nsolutions[i]] = 0

        self.Update([],Nsolutions,[])


    def AddActivating (self,activatings): #添加诱因
        Nactivatings = []

        for i in range (0,len(activatings)):
            count = 0

            for j in range (0,len(self.activatings)):
                if activatings[i] == self.activatings[j]:
                    break
                count += 1
            
            if count < len(self.activatings): #有重复
                continue

            Nactivatings.append(activatings[i])

        Nactivatings = list (set(Nactivatings)) #去除重复

        self.Update ([],[],Nactivatings)

        if len(Nactivatings) > 0: #有新诱因
            return 1
        else:
            return 0


    def AddSample (self,sample): #添加样本
        self.Data = pd.concat([self.Data,sample])
        self.datasize += 1